package com.yi.mall.goods.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.yi.mall.goods.mapper.BrandMapper;
import com.yi.mall.goods.mapper.CategoryMapper;
import com.yi.mall.common.model.Brand;
import com.yi.mall.common.model.Category;
import com.yi.mall.common.model.CategoryBrand;
import com.yi.mall.goods.mapper.CategoryBrandMapper;
import com.yi.mall.common.model.DTO.BrandDTO;
import com.yi.mall.goods.service.ICategoryBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zzy
 * @since 2025-07-10
 */
@Service
public class CategoryBrandServiceImpl extends ServiceImpl<CategoryBrandMapper, CategoryBrand> implements ICategoryBrandService {
    @Autowired
    private BrandMapper brandMapper ;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 传入三级id
     * @param id
     * @return
     */
    @Override
    public List<BrandDTO> listBythree(Integer id) {
        //1.构造条件构造器
        List<Integer> Ids = getBrandIds(id);
        List<BrandDTO> branddtos = getbranddtos(Ids);
        return branddtos;
    }

    /**
     * 传入二级id
     * @param id
     * @return
     */

    @Override
    public List<BrandDTO> listBytwo(Integer id) {
        List<Integer> Threeids = getids(id);
        List<Integer> ids = getBrandIds(Threeids);
        //1.构造条件构造器
        List<BrandDTO> branddtos = getbranddtos(ids);
        return branddtos ;
    }

    /**
     * 传入一级id
     * @param id
     * @return
     */
    @Override
    public List<BrandDTO> listByone(Integer id) {
        List<Integer> Twoids = getids(id);
        List<Integer> Threeids = getids(Twoids);
        List<Integer> ids = getBrandIds(Threeids);
        List<BrandDTO> branddtos = getbranddtos(ids);
        return branddtos ;

    }

    /**
     * 根据传入的三级id，返回该三级id下的所有品牌id
     * @param id
     * @return
     */
    public List<Integer> getBrandIds(Integer id){
        LambdaQueryChainWrapper<CategoryBrand> wrapper = new LambdaQueryChainWrapper<>(this.baseMapper);
        //2.查询
        List<CategoryBrand> list = wrapper.eq(CategoryBrand::getCategoryId, id)
                .select(CategoryBrand::getBrandId)
                .list();
        List<Integer> ids = list.stream().map(categoryBrand -> categoryBrand.getBrandId()).collect(Collectors.toList());
        return ids;

    }

    /**
     * 根据传入的ids，返回该ids下的所有品牌id和logo
     * @param ids
     * @return
     */
    public List<BrandDTO> getbranddtos(List<Integer> ids){
        //1.构造条件构造器
        LambdaQueryChainWrapper<Brand> wrapper1 = new LambdaQueryChainWrapper<>(brandMapper);
        //2.查询
        List<Brand> list1 = wrapper1.in(Brand::getId,ids)
                .select(Brand::getId,Brand::getLogo)
                .list();
        List<BrandDTO> brands = list1.stream().map(brand -> {
                    BrandDTO brandDTO = JSON.parseObject(JSON.toJSONString(brand), BrandDTO.class);
                    return brandDTO ;
                }
        ).collect(Collectors.toList());
        return brands;
    }

    /**
     * 根据传入的id，返回该id下的所有次级id列表
     * @param id
     * @return
     */
    public List<Integer> getids(Integer id){
        LambdaQueryChainWrapper<Category> wrapper = new LambdaQueryChainWrapper<>(categoryMapper);
        List<Integer> Threeids = wrapper.eq(Category::getParentId, id).select(Category::getId).list()
                .stream().map(category -> category.getId()).collect(Collectors.toList());
        return Threeids;
    }

    /**
     * 根据传入的id列表，返回所有该id列表下的次级id列表
     * @param Tids
     * @return
     */
    public List<Integer> getids(List<Integer> Tids){
        LambdaQueryChainWrapper<Category> Threewrapper = new LambdaQueryChainWrapper<>(categoryMapper);
        List<Integer> ids = Threewrapper.in(Category::getParentId, Tids).select(Category::getId).list()
                .stream().map(category -> category.getId()).collect(Collectors.toList());
        return ids;
    }

    /**
     * 根据传入的三级id列表，返回该三级id列表下的所有品牌id
     * @param Threeids
     * @return
     */
    public List<Integer> getBrandIds(List<Integer> Threeids){
        LambdaQueryChainWrapper<CategoryBrand> wrapper1 = new LambdaQueryChainWrapper<>(this.baseMapper);
        //2.查询
        List<CategoryBrand> list = wrapper1.in(CategoryBrand::getCategoryId, Threeids)
                .select(CategoryBrand::getBrandId)
                .list();
        List<Integer> ids = list.stream().map(categoryBrand -> categoryBrand.getBrandId()).distinct().collect(Collectors.toList());
        return ids;
    }

}
